package com.sanchunhui.psc.ui.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.sanchunhui.psc.R
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.find

/**
 * @Author zyx
 * @Date 2021/4/09-10:00
 * @Dscribe 消息列表主界面
 */

class MessageFragment : BaseFragment() {

    val fragments = listOf<Fragment>(AlarmsFragment(), NoticeFragment())
    val titles = listOf<String>("报警", "公告")
    lateinit var viewPager : ViewPager
    lateinit var tabLayout : TabLayout

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = View.inflate(context, R.layout.fragment_message, null)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initHeader()
        initTablayout()
    }

    private fun initTablayout() {
        viewPager = view!!.find(R.id.vp)
        tabLayout = view!!.find(R.id.tabs)

        for (i in 0 until titles.size) {
            var tabitem = tabLayout.newTab()
            tabitem.setCustomView(R.layout.item_red_dot)
            tabLayout.addTab(tabitem)
        }

        for (i in 0 until titles.size) {
            val view = tabLayout.getTabAt(i)!!.customView!!.findViewById<TextView>(R.id.text1)
            if (i==0) {
                view.setTextAppearance(context, R.style.tab_select_style)
            }
            view.setText(titles[i])
        }

        viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(p0: TabLayout.Tab?) {
                p0!!.customView?.findViewById<TextView>(R.id.text1)?.setTextColor(Color.BLACK)
                p0.customView?.findViewById<TextView>(R.id.text1)?.setTextAppearance(context, R.style.tab_select_style)
                viewPager.setCurrentItem(p0.position,true)
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {

            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
                p0!!.customView?.findViewById<TextView>(R.id.text1)?.setTextColor(Color.GRAY)
                p0.customView?.findViewById<TextView>(R.id.text1)?.setTextAppearance(context, R.style.tab_normal_style)
            }
        })

        viewPager.adapter = MessageFragmentPagerAdapter()
//        tabLayout.setupWithViewPager(viewPager)
    }

    private fun initHeader() {
        headerTitle.visibility = View.VISIBLE
        iv_message_search.visibility = View.GONE
        headerTitle.text = "消息(0)"
    }

    inner class MessageFragmentPagerAdapter : FragmentPagerAdapter(childFragmentManager) {

        override fun getPageTitle(position: Int): CharSequence? {
            return titles.get(position)
        }
        override fun getCount(): Int {
            return  titles.size
        }
        override fun getItem(position: Int): Fragment {
            return fragments[position]
//            when (position) {
//                0 -> {
//                    return AlarmsFragment()
//                }
//                1 -> {
//                    return NoticeFragment()
//                }
//                else -> return null
//            }
        }
    }
}
